package com.unknown.wificontrol.util;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Environment;
import android.provider.MediaStore;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class Util {
    public static File saveFiles(Activity mActivity, Bitmap bmp) {
        File fileDir = new File(mActivity.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "wifi");
        if (!fileDir.exists()) {
            fileDir.mkdir();
        }
        Calendar now = Calendar.getInstance();
        SimpleDateFormat folderFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        File subFolder = new File(fileDir, folderFormat.format(now.getTime()));
        if (!subFolder.exists()) {
            subFolder.mkdir();
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.getDefault());
        String fileName = String.format("WIFI-%s.jpg", dateFormat.format(now.getTime()));
        File file = new File(subFolder, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (Exception e) {
            return null;
        }
        return file;
    }
}
